﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class index : System.Web.UI.Page
{
    
    db_news dbNews = new db_news();
    db_slide dbSlide = new db_slide();
    db_blogroll blogroll = new db_blogroll();
    db_course course = new db_course();
    db_teacher teacher = new db_teacher();

    public string strcr = "";
    public bool isLogin;
    public string[] Info = new string[3];

    public string cwdNum = "";//课件次数
    public string cNum = "";//课程数量
    public string goldNum = "";
    public string sName = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["isLogin"] != null)
            {
                isLogin = Boolean.Parse(Session["isLogin"].ToString());
            }
            else
            {
                isLogin = false;
            }
            if (Session["stuId"] != null)
            {
                goldNum = new db_users().selectUserONE_id(Session["stuId"].ToString()).Tables[0].Rows[0]["gold"].ToString();
                cwdNum = new db_courseware().selectCoursewareDownByStuIdCount(Session["stuId"].ToString()).Tables[0].Rows[0]["cwdnum"].ToString();
                cNum = new db_course().selectCourseSellByStuIdCount(Session["stuId"].ToString()).Tables[0].Rows[0]["cnum"].ToString();
                Response.Redirect("../index.aspx");
            }
            
        }
        catch
        {
            
        }
        DataSet ds_newsList;
        DataSet ds_courseList;
        DataSet ds_slide;
        DataSet ds_blogroll;
        DataSet ds_teacher;


        ds_courseList = course.selectCuorseTopKind("9", "1");
        javaList.DataSource = ds_courseList.Tables[0].DefaultView;
        javaList.DataBind();
        ds_courseList = course.selectCuorseTopKind("9", "2");
        netList.DataSource = ds_courseList.Tables[0].DefaultView;
        netList.DataBind();
        ds_courseList = course.selectCuorseTopKind("9", "3");
        dataList.DataSource = ds_courseList.Tables[0].DefaultView;
        dataList.DataBind();
        ds_courseList = course.selectCuorseTopKind("9", "4");
        otherList.DataSource = ds_courseList.Tables[0].DefaultView;
        otherList.DataBind();
        ds_newsList = dbNews.selectNewsALLTop_id("6");
        news_list.DataSource = ds_newsList.Tables[0].DefaultView;
        news_list.DataBind();
        ds_slide = dbSlide.selectSlideALL();

        ds_teacher = teacher.selectTeacherALL("0");
        teacherList_1.DataSource = ds_teacher.Tables[0].DefaultView;
        teacherList_1.DataBind();
        teacherList_2.DataSource = ds_teacher.Tables[0].DefaultView;
        teacherList_2.DataBind();

        ds_courseList = course.selectCourseSellTop("6");
        hot_list.DataSource = ds_courseList.Tables[0].DefaultView;
        hot_list.DataBind();

        express_slide.DataSource = ds_slide.Tables[0].DefaultView;
        express_slide.DataBind();
        ds_blogroll = blogroll.selectBlogrollALL();
        blogrollList.DataSource = ds_blogroll.Tables[0].DefaultView;
        blogrollList.DataBind();


        db_copyright cr = new db_copyright();
        DataSet dscr = cr.selectOne();
        strcr = "Copyright &copy; " + dscr.Tables[0].Rows[0]["cryear"].ToString() + " " + dscr.Tables[0].Rows[0]["owner"].ToString() + " ALL Rights Reserved<br/><a href=\"http://www.miibeian.gov.cn\" target=\"_blank\">" + dscr.Tables[0].Rows[0]["approve"].ToString() + "</a>";
  
    }


    

    protected void ibtn_login_Click(object sender, ImageClickEventArgs e)
    {
        if (tb_userID.Text == "" || tb_userPwd.Text == "" || tb_code.Text == "") {
            Response.Write("<script language=javascript>alert('请填写登陆信息！');top.location.href='../index.aspx';</script>");
        }
        string name = Tools.Clearstr(tb_userID.Text, 20);
        string pwd = Tools.Clearstr(tb_userPwd.Text, 20);
        string code = Tools.Clearstr(tb_code.Text, 20);
        bool isErrorCode = true;
        try
        {

            if (code != Session["userCode"].ToString()) //判断验证码是否正确
            {
                isErrorCode = true;//如果不正确，则为true
            }
            else
            {
                isErrorCode = false;
            }
        }
        catch
        {
            Response.Write("<script language=javascript>alert('验证码超时，请重新登录！');top.location.href='../index.aspx';</script>");
        }

        if (isErrorCode)
        {
            Response.Write("<script language=javascript>alert('验证码错误！！！');top.location.href='../index.aspx';</script>");
        }
        else
        {
            db_users user = new db_users();
            Info = user.isLogin(name, Tools.mymd5(pwd));
            if (Info != null)
            {
                if (Info[0].Equals("noPass"))
                {
                    Response.Write("<script language=javascript>alert('该用户已停用。');top.location.href='index.aspx';</script>");
                }
                else
                {
                    Session["stuId"] = Info[0];
                    Session["stuName"] = name;
                    Session["lastTime"] = Info[2];
                    Session["sex"] = Info[3];
                    Session["isLogin"] = true;
                    Response.Redirect("index.aspx");
                }
            }
            else
            {
                Session["stuId"] = null;
                Session["stuName"] = null;
                Session["lastTime"] = null;
                Session["sex"] = null;
                Session["isLogin"] = false;
                Response.Write("<script language=javascript>alert('用户名或密码错误，登陆不成功！！！');top.location.href='index.aspx';</script>");
            }
        }
    }

    protected void ibtn_register_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("~/stu/register.aspx");
    }
}